{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de53995b-32ed-4722-8cac-ba104c8efacb",
   "metadata": {},
   "source": [
    "# 导入环境"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ee496fba-5643-40b4-ae82-c15b56d48c8b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing to /root/.config/pip/pip.conf\n",
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: modelscope==1.25.0 in ./miniconda3/lib/python3.12/site-packages (1.25.0)\n",
      "Requirement already satisfied: requests>=2.25 in ./miniconda3/lib/python3.12/site-packages (from modelscope==1.25.0) (2.32.4)\n",
      "Requirement already satisfied: tqdm>=4.64.0 in ./miniconda3/lib/python3.12/site-packages (from modelscope==1.25.0) (4.67.1)\n",
      "Requirement already satisfied: urllib3>=1.26 in ./miniconda3/lib/python3.12/site-packages (from modelscope==1.25.0) (2.1.0)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.25->modelscope==1.25.0) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.25->modelscope==1.25.0) (3.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.25->modelscope==1.25.0) (2024.2.2)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: transformers==4.51.3 in ./miniconda3/lib/python3.12/site-packages (4.51.3)\n",
      "Requirement already satisfied: filelock in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (3.16.1)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.30.0 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (0.33.1)\n",
      "Requirement already satisfied: numpy>=1.17 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (2.1.3)\n",
      "Requirement already satisfied: packaging>=20.0 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (23.2)\n",
      "Requirement already satisfied: pyyaml>=5.1 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (6.0.2)\n",
      "Requirement already satisfied: regex!=2019.12.17 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (2024.11.6)\n",
      "Requirement already satisfied: requests in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (2.32.4)\n",
      "Requirement already satisfied: tokenizers<0.22,>=0.21 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (0.21.2)\n",
      "Requirement already satisfied: safetensors>=0.4.3 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (0.5.3)\n",
      "Requirement already satisfied: tqdm>=4.27 in ./miniconda3/lib/python3.12/site-packages (from transformers==4.51.3) (4.67.1)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.30.0->transformers==4.51.3) (2024.10.0)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.30.0->transformers==4.51.3) (4.12.2)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub<1.0,>=0.30.0->transformers==4.51.3) (1.1.5)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in ./miniconda3/lib/python3.12/site-packages (from requests->transformers==4.51.3) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in ./miniconda3/lib/python3.12/site-packages (from requests->transformers==4.51.3) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in ./miniconda3/lib/python3.12/site-packages (from requests->transformers==4.51.3) (2.1.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in ./miniconda3/lib/python3.12/site-packages (from requests->transformers==4.51.3) (2024.2.2)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: accelerate==1.6.0 in ./miniconda3/lib/python3.12/site-packages (1.6.0)\n",
      "Requirement already satisfied: numpy<3.0.0,>=1.17 in ./miniconda3/lib/python3.12/site-packages (from accelerate==1.6.0) (2.1.3)\n",
      "Requirement already satisfied: packaging>=20.0 in ./miniconda3/lib/python3.12/site-packages (from accelerate==1.6.0) (23.2)\n",
      "Requirement already satisfied: psutil in ./miniconda3/lib/python3.12/site-packages (from accelerate==1.6.0) (6.1.0)\n",
      "Requirement already satisfied: pyyaml in ./miniconda3/lib/python3.12/site-packages (from accelerate==1.6.0) (6.0.2)\n",
      "Requirement already satisfied: torch>=2.0.0 in ./miniconda3/lib/python3.12/site-packages (from accelerate==1.6.0) (2.5.1+cu124)\n",
      "Requirement already satisfied: huggingface-hub>=0.21.0 in ./miniconda3/lib/python3.12/site-packages (from accelerate==1.6.0) (0.33.1)\n",
      "Requirement already satisfied: safetensors>=0.4.3 in ./miniconda3/lib/python3.12/site-packages (from accelerate==1.6.0) (0.5.3)\n",
      "Requirement already satisfied: filelock in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==1.6.0) (3.16.1)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==1.6.0) (2024.10.0)\n",
      "Requirement already satisfied: requests in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==1.6.0) (2.32.4)\n",
      "Requirement already satisfied: tqdm>=4.42.1 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==1.6.0) (4.67.1)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==1.6.0) (4.12.2)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.21.0->accelerate==1.6.0) (1.1.5)\n",
      "Requirement already satisfied: networkx in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (3.4.2)\n",
      "Requirement already satisfied: jinja2 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (3.1.4)\n",
      "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (12.4.127)\n",
      "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (12.4.127)\n",
      "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (12.4.127)\n",
      "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (9.1.0.70)\n",
      "Requirement already satisfied: nvidia-cublas-cu12==12.4.5.8 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (12.4.5.8)\n",
      "Requirement already satisfied: nvidia-cufft-cu12==11.2.1.3 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (11.2.1.3)\n",
      "Requirement already satisfied: nvidia-curand-cu12==10.3.5.147 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (10.3.5.147)\n",
      "Requirement already satisfied: nvidia-cusolver-cu12==11.6.1.9 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (11.6.1.9)\n",
      "Requirement already satisfied: nvidia-cusparse-cu12==12.3.1.170 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (12.3.1.170)\n",
      "Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (2.21.5)\n",
      "Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (12.4.127)\n",
      "Requirement already satisfied: nvidia-nvjitlink-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (12.4.127)\n",
      "Requirement already satisfied: triton==3.1.0 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (3.1.0)\n",
      "Requirement already satisfied: setuptools in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (69.5.1)\n",
      "Requirement already satisfied: sympy==1.13.1 in ./miniconda3/lib/python3.12/site-packages (from torch>=2.0.0->accelerate==1.6.0) (1.13.1)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in ./miniconda3/lib/python3.12/site-packages (from sympy==1.13.1->torch>=2.0.0->accelerate==1.6.0) (1.3.0)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in ./miniconda3/lib/python3.12/site-packages (from jinja2->torch>=2.0.0->accelerate==1.6.0) (3.0.2)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.6.0) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.6.0) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.6.0) (2.1.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface-hub>=0.21.0->accelerate==1.6.0) (2024.2.2)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: datasets==3.5.1 in ./miniconda3/lib/python3.12/site-packages (3.5.1)\n",
      "Requirement already satisfied: filelock in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (3.16.1)\n",
      "Requirement already satisfied: numpy>=1.17 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (2.1.3)\n",
      "Requirement already satisfied: pyarrow>=15.0.0 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (20.0.0)\n",
      "Requirement already satisfied: dill<0.3.9,>=0.3.0 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (0.3.8)\n",
      "Requirement already satisfied: pandas in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (2.3.0)\n",
      "Requirement already satisfied: requests>=2.32.2 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (2.32.4)\n",
      "Requirement already satisfied: tqdm>=4.66.3 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (4.67.1)\n",
      "Requirement already satisfied: xxhash in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (3.5.0)\n",
      "Requirement already satisfied: multiprocess<0.70.17 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (0.70.16)\n",
      "Requirement already satisfied: fsspec<=2025.3.0,>=2023.1.0 in ./miniconda3/lib/python3.12/site-packages (from fsspec[http]<=2025.3.0,>=2023.1.0->datasets==3.5.1) (2024.10.0)\n",
      "Requirement already satisfied: aiohttp in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (3.12.13)\n",
      "Requirement already satisfied: huggingface-hub>=0.24.0 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (0.33.1)\n",
      "Requirement already satisfied: packaging in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (23.2)\n",
      "Requirement already satisfied: pyyaml>=5.1 in ./miniconda3/lib/python3.12/site-packages (from datasets==3.5.1) (6.0.2)\n",
      "Requirement already satisfied: aiohappyeyeballs>=2.5.0 in ./miniconda3/lib/python3.12/site-packages (from aiohttp->datasets==3.5.1) (2.6.1)\n",
      "Requirement already satisfied: aiosignal>=1.1.2 in ./miniconda3/lib/python3.12/site-packages (from aiohttp->datasets==3.5.1) (1.3.2)\n",
      "Requirement already satisfied: attrs>=17.3.0 in ./miniconda3/lib/python3.12/site-packages (from aiohttp->datasets==3.5.1) (24.2.0)\n",
      "Requirement already satisfied: frozenlist>=1.1.1 in ./miniconda3/lib/python3.12/site-packages (from aiohttp->datasets==3.5.1) (1.7.0)\n",
      "Requirement already satisfied: multidict<7.0,>=4.5 in ./miniconda3/lib/python3.12/site-packages (from aiohttp->datasets==3.5.1) (6.6.2)\n",
      "Requirement already satisfied: propcache>=0.2.0 in ./miniconda3/lib/python3.12/site-packages (from aiohttp->datasets==3.5.1) (0.3.2)\n",
      "Requirement already satisfied: yarl<2.0,>=1.17.0 in ./miniconda3/lib/python3.12/site-packages (from aiohttp->datasets==3.5.1) (1.20.1)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.24.0->datasets==3.5.1) (4.12.2)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in ./miniconda3/lib/python3.12/site-packages (from huggingface-hub>=0.24.0->datasets==3.5.1) (1.1.5)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.32.2->datasets==3.5.1) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.32.2->datasets==3.5.1) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.32.2->datasets==3.5.1) (2.1.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.32.2->datasets==3.5.1) (2024.2.2)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in ./miniconda3/lib/python3.12/site-packages (from pandas->datasets==3.5.1) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in ./miniconda3/lib/python3.12/site-packages (from pandas->datasets==3.5.1) (2025.2)\n",
      "Requirement already satisfied: tzdata>=2022.7 in ./miniconda3/lib/python3.12/site-packages (from pandas->datasets==3.5.1) (2025.2)\n",
      "Requirement already satisfied: six>=1.5 in ./miniconda3/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas->datasets==3.5.1) (1.16.0)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: peft==0.15.2 in ./miniconda3/lib/python3.12/site-packages (0.15.2)\n",
      "Requirement already satisfied: numpy>=1.17 in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (2.1.3)\n",
      "Requirement already satisfied: packaging>=20.0 in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (23.2)\n",
      "Requirement already satisfied: psutil in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (6.1.0)\n",
      "Requirement already satisfied: pyyaml in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (6.0.2)\n",
      "Requirement already satisfied: torch>=1.13.0 in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (2.5.1+cu124)\n",
      "Requirement already satisfied: transformers in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (4.51.3)\n",
      "Requirement already satisfied: tqdm in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (4.67.1)\n",
      "Requirement already satisfied: accelerate>=0.21.0 in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (1.6.0)\n",
      "Requirement already satisfied: safetensors in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (0.5.3)\n",
      "Requirement already satisfied: huggingface_hub>=0.25.0 in ./miniconda3/lib/python3.12/site-packages (from peft==0.15.2) (0.33.1)\n",
      "Requirement already satisfied: filelock in ./miniconda3/lib/python3.12/site-packages (from huggingface_hub>=0.25.0->peft==0.15.2) (3.16.1)\n",
      "Requirement already satisfied: fsspec>=2023.5.0 in ./miniconda3/lib/python3.12/site-packages (from huggingface_hub>=0.25.0->peft==0.15.2) (2024.10.0)\n",
      "Requirement already satisfied: requests in ./miniconda3/lib/python3.12/site-packages (from huggingface_hub>=0.25.0->peft==0.15.2) (2.32.4)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.3 in ./miniconda3/lib/python3.12/site-packages (from huggingface_hub>=0.25.0->peft==0.15.2) (4.12.2)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in ./miniconda3/lib/python3.12/site-packages (from huggingface_hub>=0.25.0->peft==0.15.2) (1.1.5)\n",
      "Requirement already satisfied: networkx in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (3.4.2)\n",
      "Requirement already satisfied: jinja2 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (3.1.4)\n",
      "Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (12.4.127)\n",
      "Requirement already satisfied: nvidia-cuda-runtime-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (12.4.127)\n",
      "Requirement already satisfied: nvidia-cuda-cupti-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (12.4.127)\n",
      "Requirement already satisfied: nvidia-cudnn-cu12==9.1.0.70 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (9.1.0.70)\n",
      "Requirement already satisfied: nvidia-cublas-cu12==12.4.5.8 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (12.4.5.8)\n",
      "Requirement already satisfied: nvidia-cufft-cu12==11.2.1.3 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (11.2.1.3)\n",
      "Requirement already satisfied: nvidia-curand-cu12==10.3.5.147 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (10.3.5.147)\n",
      "Requirement already satisfied: nvidia-cusolver-cu12==11.6.1.9 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (11.6.1.9)\n",
      "Requirement already satisfied: nvidia-cusparse-cu12==12.3.1.170 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (12.3.1.170)\n",
      "Requirement already satisfied: nvidia-nccl-cu12==2.21.5 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (2.21.5)\n",
      "Requirement already satisfied: nvidia-nvtx-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (12.4.127)\n",
      "Requirement already satisfied: nvidia-nvjitlink-cu12==12.4.127 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (12.4.127)\n",
      "Requirement already satisfied: triton==3.1.0 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (3.1.0)\n",
      "Requirement already satisfied: setuptools in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (69.5.1)\n",
      "Requirement already satisfied: sympy==1.13.1 in ./miniconda3/lib/python3.12/site-packages (from torch>=1.13.0->peft==0.15.2) (1.13.1)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in ./miniconda3/lib/python3.12/site-packages (from sympy==1.13.1->torch>=1.13.0->peft==0.15.2) (1.3.0)\n",
      "Requirement already satisfied: regex!=2019.12.17 in ./miniconda3/lib/python3.12/site-packages (from transformers->peft==0.15.2) (2024.11.6)\n",
      "Requirement already satisfied: tokenizers<0.22,>=0.21 in ./miniconda3/lib/python3.12/site-packages (from transformers->peft==0.15.2) (0.21.2)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in ./miniconda3/lib/python3.12/site-packages (from jinja2->torch>=1.13.0->peft==0.15.2) (3.0.2)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub>=0.25.0->peft==0.15.2) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub>=0.25.0->peft==0.15.2) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub>=0.25.0->peft==0.15.2) (2.1.0)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in ./miniconda3/lib/python3.12/site-packages (from requests->huggingface_hub>=0.25.0->peft==0.15.2) (2024.2.2)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mLooking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: swanlab==0.5.7 in ./miniconda3/lib/python3.12/site-packages (0.5.7)\n",
      "Requirement already satisfied: boto3>=1.35.49 in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (1.38.46)\n",
      "Requirement already satisfied: botocore in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (1.38.46)\n",
      "Requirement already satisfied: click in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (8.2.1)\n",
      "Requirement already satisfied: psutil>=5.0.0 in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (6.1.0)\n",
      "Requirement already satisfied: pydantic>=2.9.0 in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (2.11.7)\n",
      "Requirement already satisfied: pynvml in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (12.0.0)\n",
      "Requirement already satisfied: pyyaml in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (6.0.2)\n",
      "Requirement already satisfied: requests>=2.25.0 in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (2.32.4)\n",
      "Requirement already satisfied: setuptools in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (69.5.1)\n",
      "Requirement already satisfied: swankit==0.1.7 in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (0.1.7)\n",
      "Requirement already satisfied: urllib3>=1.26.0 in ./miniconda3/lib/python3.12/site-packages (from swanlab==0.5.7) (2.1.0)\n",
      "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in ./miniconda3/lib/python3.12/site-packages (from boto3>=1.35.49->swanlab==0.5.7) (1.0.1)\n",
      "Requirement already satisfied: s3transfer<0.14.0,>=0.13.0 in ./miniconda3/lib/python3.12/site-packages (from boto3>=1.35.49->swanlab==0.5.7) (0.13.0)\n",
      "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in ./miniconda3/lib/python3.12/site-packages (from botocore->swanlab==0.5.7) (2.9.0.post0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in ./miniconda3/lib/python3.12/site-packages (from pydantic>=2.9.0->swanlab==0.5.7) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in ./miniconda3/lib/python3.12/site-packages (from pydantic>=2.9.0->swanlab==0.5.7) (2.33.2)\n",
      "Requirement already satisfied: typing-extensions>=4.12.2 in ./miniconda3/lib/python3.12/site-packages (from pydantic>=2.9.0->swanlab==0.5.7) (4.12.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in ./miniconda3/lib/python3.12/site-packages (from pydantic>=2.9.0->swanlab==0.5.7) (0.4.1)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.25.0->swanlab==0.5.7) (2.0.4)\n",
      "Requirement already satisfied: idna<4,>=2.5 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.25.0->swanlab==0.5.7) (3.7)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in ./miniconda3/lib/python3.12/site-packages (from requests>=2.25.0->swanlab==0.5.7) (2024.2.2)\n",
      "Requirement already satisfied: nvidia-ml-py<13.0.0a0,>=12.0.0 in ./miniconda3/lib/python3.12/site-packages (from pynvml->swanlab==0.5.7) (12.575.51)\n",
      "Requirement already satisfied: six>=1.5 in ./miniconda3/lib/python3.12/site-packages (from python-dateutil<3.0.0,>=2.1->botocore->swanlab==0.5.7) (1.16.0)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "!pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple\n",
    "\n",
    "!pip install modelscope==1.25.0\n",
    "!pip install transformers==4.51.3\n",
    "!pip install accelerate==1.6.0\n",
    "!pip install datasets==3.5.1\n",
    "!pip install peft==0.15.2\n",
    "!pip install swanlab==0.5.7\n",
    "!pip install tiktoken"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "26a8ffdd-1d25-415f-af42-284d6f63c9ee",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-06-30 21:32:49,356 - modelscope - WARNING - Using branch: master as version is unstable, use with caution\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading Model from https://www.modelscope.cn to directory: /root/autodl-tmp/Tencent-Hunyuan/Hunyuan-A13B-Instruct\n",
      "模型下载完成，保存路径为：/root/autodl-tmp/Tencent-Hunyuan/Hunyuan-A13B-Instruct\n"
     ]
    }
   ],
   "source": [
    "from modelscope import snapshot_download\n",
    "model_dir = snapshot_download('Tencent-Hunyuan/Hunyuan-A13B-Instruct', cache_dir='/root/autodl-tmp', revision='master') ###可以根据自己需要修改下载模型所在的存储路径:cache_dir\n",
    "\n",
    "print(f\"模型下载完成，保存路径为：{model_dir}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "52fac949-4150-4091-b0c3-2968ab5e385c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from datasets import Dataset\n",
    "import pandas as pd\n",
    "from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForSeq2Seq, TrainingArguments, Trainer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e098d9eb",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 将JSON文件转换为CSV文件\n",
    "df = pd.read_json('./huanhuan.json') ###此处是json数据文件的路径根据实际情况修改\n",
    "ds = Dataset.from_pandas(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8ac92d42-efae-49b1-a00e-ccaa75b98938",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'instruction': ['小姐，别的秀女都在求中选，唯有咱们小姐想被撂牌子，菩萨一定记得真真儿的——',\n",
       "  '这个温太医啊，也是古怪，谁不知太医不得皇命不能为皇族以外的人请脉诊病，他倒好，十天半月便往咱们府里跑。',\n",
       "  '嬛妹妹，刚刚我去府上请脉，听甄伯母说你来这里进香了。'],\n",
       " 'input': ['', '', ''],\n",
       " 'output': ['嘘——都说许愿说破是不灵的。', '你们俩话太多了，我该和温太医要一剂药，好好治治你们。', '出来走走，也是散心。']}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ds[:3]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51d05e5d-d14e-4f03-92be-9a9677d41918",
   "metadata": {},
   "source": [
    "# 处理数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "74ee5a67-2e55-4974-b90e-cbf492de500a",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "tokenizer = AutoTokenizer.from_pretrained('/root/autodl-tmp/Tencent-Hunyuan/Hunyuan-A13B-Instruct', trust_remote_code=True) ###模型文件路径根据实际路径修改"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "714f9eab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<|startoftext|>===system_message_test===<|extra_4|>===user_message_test===<|extra_0|>===assistant_message_test===<|eos|>\n"
     ]
    }
   ],
   "source": [
    "messages = [\n",
    "    {\"role\": \"system\", \"content\": \"===system_message_test===\"},\n",
    "    {\"role\": \"user\", \"content\": \"===user_message_test===\"},\n",
    "    {\"role\": \"assistant\", \"content\": \"===assistant_message_test===\"},\n",
    "]\n",
    "\n",
    "text = tokenizer.apply_chat_template(\n",
    "    messages,\n",
    "    tokenize=False,\n",
    "    add_generation_prompt=True,\n",
    "    enable_thinking=True\n",
    ")\n",
    "print(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2503a5fa-9621-4495-9035-8e7ef6525691",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "def process_func(example):\n",
    "    MAX_LENGTH = 1024 # 设置最大序列长度为1024个token\n",
    "    input_ids, attention_mask, labels = [], [], [] # 初始化返回值\n",
    "    # 适配chat_template\n",
    "    instruction = tokenizer(\n",
    "        f\"<s><|im_start|>system\\n现在你要扮演皇帝身边的女人--甄嬛<|im_end|>\\n\" \n",
    "        f\"<|im_start|>user\\n{example['instruction'] + example['input']}<|im_end|>\\n\"  \n",
    "        f\"<|im_start|>assistant\\n<think>\\n\\n</think>\\n\\n\",  \n",
    "        add_special_tokens=False   \n",
    "    )\n",
    "    response = tokenizer(f\"{example['output']}\", add_special_tokens=False)\n",
    "    # 将instructio部分和response部分的input_ids拼接，并在末尾添加eos token作为标记结束的token\n",
    "    input_ids = instruction[\"input_ids\"] + response[\"input_ids\"] + [tokenizer.pad_token_id]\n",
    "    # 注意力掩码，表示模型需要关注的位置\n",
    "    attention_mask = instruction[\"attention_mask\"] + response[\"attention_mask\"] + [1]\n",
    "    # 对于instruction，使用-100表示这些位置不计算loss（即模型不需要预测这部分）\n",
    "    labels = [-100] * len(instruction[\"input_ids\"]) + response[\"input_ids\"] + [tokenizer.pad_token_id]  \n",
    "    if len(input_ids) > MAX_LENGTH:  # 超出最大序列长度截断\n",
    "        input_ids = input_ids[:MAX_LENGTH]\n",
    "        attention_mask = attention_mask[:MAX_LENGTH]\n",
    "        labels = labels[:MAX_LENGTH]\n",
    "    return {\n",
    "        \"input_ids\": input_ids,\n",
    "        \"attention_mask\": attention_mask,\n",
    "        \"labels\": labels\n",
    "    }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "84f870d6-73a9-4b0f-8abf-687b32224ad8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "857150385b4647e3934b80cb2828d1d4",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Map:   0%|          | 0/3729 [00:00<?, ? examples/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['input_ids', 'attention_mask', 'labels'],\n",
       "    num_rows: 3729\n",
       "})"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tokenized_id = ds.map(process_func, remove_columns=ds.column_names)\n",
    "tokenized_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "1f7e15a0-4d9a-4935-9861-00cc472654b1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<s><|im_start|>system\n",
      "现在你要扮演皇帝身边的女人--甄嬛<|im_end|>\n",
      "<|im_start|>user\n",
      "小姐，别的秀女都在求中选，唯有咱们小姐想被撂牌子，菩萨一定记得真真儿的——<|im_end|>\n",
      "<|im_start|>assistant\n",
      "<think>\n",
      "\n",
      "</think>\n",
      "\n",
      "嘘——都说许愿说破是不灵的。<|pad|>\n"
     ]
    }
   ],
   "source": [
    "print(tokenizer.decode(tokenized_id[0]['input_ids']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "97f16f66-324a-454f-8cc3-ef23b100ecff",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你们俩话太多了，我该和温太医要一剂药，好好治治你们。<|pad|>\n"
     ]
    }
   ],
   "source": [
    "print(tokenizer.decode(list(filter(lambda x: x != -100, tokenized_id[1][\"labels\"]))))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "424823a8-ed0d-4309-83c8-3f6b1cdf274c",
   "metadata": {},
   "source": [
    "# 创建模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "170764e5-d899-4ef4-8c53-36f6dec0d198",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "41defcd7fd0b47bea81890405610fc90",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/4 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "HunYuanMoEV1ForCausalLM(\n",
       "  (model): HunYuanModel(\n",
       "    (embed_tokens): Embedding(128167, 4096, padding_idx=127961)\n",
       "    (layers): ModuleList(\n",
       "      (0-31): 32 x HunYuanDecoderLayer(\n",
       "        (self_attn): HunYuanSdpaAttention(\n",
       "          (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
       "          (k_proj): Linear(in_features=4096, out_features=1024, bias=False)\n",
       "          (v_proj): Linear(in_features=4096, out_features=1024, bias=False)\n",
       "          (o_proj): Linear(in_features=4096, out_features=4096, bias=False)\n",
       "          (query_layernorm): HunYuanRMSNorm()\n",
       "          (key_layernorm): HunYuanRMSNorm()\n",
       "          (rotary_emb): HunYuanDynamicNTKAlphaRotaryEmbedding()\n",
       "        )\n",
       "        (mlp): HunYuanMoE(\n",
       "          (shared_mlp): HunYuanMLP(\n",
       "            (gate_proj): Linear(in_features=4096, out_features=3072, bias=False)\n",
       "            (up_proj): Linear(in_features=4096, out_features=3072, bias=False)\n",
       "            (down_proj): Linear(in_features=3072, out_features=4096, bias=False)\n",
       "            (act_fn): SiLU()\n",
       "          )\n",
       "          (gate): HunYuanTopKGate(\n",
       "            (wg): Linear(in_features=4096, out_features=64, bias=False)\n",
       "          )\n",
       "          (experts): ModuleList(\n",
       "            (0-63): 64 x HunYuanMLP(\n",
       "              (gate_proj): Linear(in_features=4096, out_features=3072, bias=False)\n",
       "              (up_proj): Linear(in_features=4096, out_features=3072, bias=False)\n",
       "              (down_proj): Linear(in_features=3072, out_features=4096, bias=False)\n",
       "              (act_fn): SiLU()\n",
       "            )\n",
       "          )\n",
       "        )\n",
       "        (input_layernorm): HunYuanRMSNorm()\n",
       "        (post_attention_layernorm): HunYuanRMSNorm()\n",
       "      )\n",
       "    )\n",
       "    (norm): HunYuanRMSNorm()\n",
       "  )\n",
       "  (lm_head): Linear(in_features=4096, out_features=128167, bias=False)\n",
       ")"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "model = AutoModelForCausalLM.from_pretrained('/root/autodl-tmp/Tencent-Hunyuan/Hunyuan-A13B-Instruct', device_map=\"auto\",torch_dtype=torch.bfloat16, trust_remote_code=True)\n",
    "model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2323eac7-37d5-4288-8bc5-79fac7113402",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "#model.enable_input_require_grads() # 开启梯度检查点时，要执行该方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f808b05c-f2cb-48cf-a80d-0c42be6051c7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.bfloat16"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.dtype"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13d71257-3c1c-4303-8ff8-af161ebc2cf1",
   "metadata": {},
   "source": [
    "# lora "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2d304ae2-ab60-4080-a80d-19cac2e3ade3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LoraConfig(task_type=<TaskType.CAUSAL_LM: 'CAUSAL_LM'>, peft_type=<PeftType.LORA: 'LORA'>, auto_mapping=None, base_model_name_or_path=None, revision=None, inference_mode=False, r=8, target_modules={'v_proj', 'o_proj', 'k_proj', 'down_proj', 'q_proj', 'up_proj', 'gate_proj'}, exclude_modules=None, lora_alpha=32, lora_dropout=0.1, fan_in_fan_out=False, bias='none', use_rslora=False, modules_to_save=None, init_lora_weights=True, layers_to_transform=None, layers_pattern=None, rank_pattern={}, alpha_pattern={}, megatron_config=None, megatron_core='megatron.core', trainable_token_indices=None, loftq_config={}, eva_config=None, corda_config=None, use_dora=False, layer_replication=None, runtime_config=LoraRuntimeConfig(ephemeral_gpu_offload=False), lora_bias=False)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from peft import LoraConfig, TaskType, get_peft_model\n",
    "\n",
    "config = LoraConfig(\n",
    "    task_type=TaskType.CAUSAL_LM, \n",
    "    target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\", \"gate_proj\", \"up_proj\", \"down_proj\"],\n",
    "    inference_mode=False, # 训练模式\n",
    "    r=8, # Lora 秩\n",
    "    lora_alpha=32, # Lora alaph，具体作用参见 Lora 原理\n",
    "    lora_dropout=0.1# Dropout 比例\n",
    ")\n",
    "config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "2c2489c5-eaab-4e1f-b06a-c3f914b4bf8e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "LoraConfig(task_type=<TaskType.CAUSAL_LM: 'CAUSAL_LM'>, peft_type=<PeftType.LORA: 'LORA'>, auto_mapping=None, base_model_name_or_path='/root/autodl-tmp/Tencent-Hunyuan/Hunyuan-A13B-Instruct', revision=None, inference_mode=False, r=8, target_modules={'v_proj', 'o_proj', 'k_proj', 'down_proj', 'q_proj', 'up_proj', 'gate_proj'}, exclude_modules=None, lora_alpha=32, lora_dropout=0.1, fan_in_fan_out=False, bias='none', use_rslora=False, modules_to_save=None, init_lora_weights=True, layers_to_transform=None, layers_pattern=None, rank_pattern={}, alpha_pattern={}, megatron_config=None, megatron_core='megatron.core', trainable_token_indices=None, loftq_config={}, eva_config=None, corda_config=None, use_dora=False, layer_replication=None, runtime_config=LoraRuntimeConfig(ephemeral_gpu_offload=False), lora_bias=False)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model = get_peft_model(model, config)\n",
    "config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ebf5482b-fab9-4eb3-ad88-c116def4be12",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "trainable params: 364,642,304 || all params: 80,757,825,536 || trainable%: 0.4515\n"
     ]
    }
   ],
   "source": [
    "model.print_trainable_parameters()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ca055683-837f-4865-9c57-9164ba60c00f",
   "metadata": {},
   "source": [
    "# 配置训练参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "7e76bbff-15fd-4995-a61d-8364dc5e9ea0",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "args = TrainingArguments(\n",
    "    output_dir=\"./output/Hunyuan-A13B-Instruct\",\n",
    "    per_device_train_batch_size=4,\n",
    "    gradient_accumulation_steps=4,\n",
    "    logging_steps=10,\n",
    "    num_train_epochs=1,\n",
    "    save_steps=100, \n",
    "    learning_rate=1e-4,\n",
    "    save_on_each_node=True,\n",
    "    gradient_checkpointing=True,\n",
    "    report_to=\"none\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "9faedcb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import swanlab\n",
    "from swanlab.integration.transformers import SwanLabCallback\n",
    "\n",
    "# 实例化SwanLabCallback\n",
    "swanlab_callback = SwanLabCallback(\n",
    "    project=\"Hunyuan-A13B-Instruct-Lora\", \n",
    "    experiment_name=\"Hunyuan-A13B-Instruct-LoRA-experiment\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "f142cb9c-ad99-48e6-ba86-6df198f9ed96",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "No label_names provided for model class `PeftModelForCausalLM`. Since `PeftModel` hides base models input arguments, if label_names is not given, label_names can't be set automatically within `Trainer`. Note that empty label_names list will be used instead.\n"
     ]
    }
   ],
   "source": [
    "trainer = Trainer(\n",
    "    model=model,\n",
    "    args=args,\n",
    "    train_dataset=tokenized_id,\n",
    "    data_collator=DataCollatorForSeq2Seq(tokenizer=tokenizer, padding=True),\n",
    "    callbacks=[swanlab_callback]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "aec9bc36-b297-45af-99e1-d4c4d82be081",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING:transformers_modules.Hunyuan-A13B-Instruct.hunyuan:`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`...\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "    <div>\n",
       "      \n",
       "      <progress value='233' max='233' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
       "      [233/233 1:11:39, Epoch 0/1]\n",
       "    </div>\n",
       "    <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       " <tr style=\"text-align: left;\">\n",
       "      <th>Step</th>\n",
       "      <th>Training Loss</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>10</td>\n",
       "      <td>4.914000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>20</td>\n",
       "      <td>3.678600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>30</td>\n",
       "      <td>3.466400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>40</td>\n",
       "      <td>3.253500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>50</td>\n",
       "      <td>3.241300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>60</td>\n",
       "      <td>3.218700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>70</td>\n",
       "      <td>3.248800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>80</td>\n",
       "      <td>3.360800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>90</td>\n",
       "      <td>3.388900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>100</td>\n",
       "      <td>3.278800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>110</td>\n",
       "      <td>3.210900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>120</td>\n",
       "      <td>3.230900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>130</td>\n",
       "      <td>3.187000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>140</td>\n",
       "      <td>3.161500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>150</td>\n",
       "      <td>3.279400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>160</td>\n",
       "      <td>3.238500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>170</td>\n",
       "      <td>3.221000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>180</td>\n",
       "      <td>3.054100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>190</td>\n",
       "      <td>3.097900</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>200</td>\n",
       "      <td>3.170800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>210</td>\n",
       "      <td>3.069500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>220</td>\n",
       "      <td>3.107300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>230</td>\n",
       "      <td>3.139400</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table><p>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "TrainOutput(global_step=233, training_loss=3.3132195984345136, metrics={'train_runtime': 4321.001, 'train_samples_per_second': 0.863, 'train_steps_per_second': 0.054, 'total_flos': 2.225441122839429e+17, 'train_loss': 3.3132195984345136, 'epoch': 0.9989281886387996})"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainer.train()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8abb2327-458e-4e96-ac98-2141b5b97c8e",
   "metadata": {},
   "source": [
    "# 合并加载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bd2a415a-a9ad-49ea-877f-243558a83bfc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "e3aa00b816d34bed96be1ddf2f991187",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Loading checkpoint shards:   0%|          | 0/4 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some parameters are on the meta device because they were offloaded to the cpu.\n",
      "/root/miniconda3/lib/python3.12/site-packages/accelerate/utils/modeling.py:1569: UserWarning: Current model requires 520101632 bytes of buffer for offloaded layers, which seems does not fit any GPU's remaining memory. If you are experiencing a OOM later, please consider using offload_buffers=True.\n",
      "  warnings.warn(\n",
      "Some parameters are on the meta device because they were offloaded to the cpu.\n"
     ]
    }
   ],
   "source": [
    "from transformers import AutoModelForCausalLM, AutoTokenizer\n",
    "import torch\n",
    "from peft import PeftModel\n",
    "\n",
    "mode_path = '/root/autodl-tmp/Tencent-Hunyuan/Hunyuan-A13B-Instruct'\n",
    "lora_path = '/root/output/Hunyuan-A13B-Instruct/checkpoint-233' # 这里改称你的 lora 输出对应 checkpoint 地址\n",
    "\n",
    "# 加载tokenizer\n",
    "tokenizer = AutoTokenizer.from_pretrained(mode_path, trust_remote_code=True)\n",
    "\n",
    "# 加载模型\n",
    "model = AutoModelForCausalLM.from_pretrained(mode_path, device_map=\"auto\",torch_dtype=torch.bfloat16, trust_remote_code=True)\n",
    "\n",
    "# 加载lora权重\n",
    "model = PeftModel.from_pretrained(model, model_id=lora_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5e8cc4b6-6f7c-42d2-bbf8-71b4bb0a0e79",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "The `seen_tokens` attribute is deprecated and will be removed in v4.41. Use the `cache_position` model input instead.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "answer_content:I'm HunYuan, Tencent's AI assistant. I'm here to help you with any questions or tasks you have. How can I assist you today?\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "prompt = \"你是谁？\"\n",
    "messages = [\n",
    "    {\"role\": \"user\", \"content\": \"who are you?\"},\n",
    "]\n",
    "tokenized_chat = tokenizer.apply_chat_template(messages, tokenize=True, return_tensors=\"pt\",\n",
    "                                                enable_thinking=False # Toggle thinking mode (default: True)\n",
    "                                                )\n",
    "                                                \n",
    "outputs = model.generate(tokenized_chat.to(model.device), max_new_tokens=4096)\n",
    "\n",
    "\n",
    "output_text = tokenizer.decode(outputs[0])\n",
    "\n",
    "answer_pattern = r'<answer>(.*?)</answer>'\n",
    "answer_matches = re.findall(answer_pattern, output_text, re.DOTALL)\n",
    "\n",
    "answer_content = [match.strip() for match in answer_matches][0]\n",
    "print(f\"answer_content:{answer_content}\\n\\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
